package com.example.springbootdemo.validate;

import com.example.springbootdemo.rpc.CacheGateway;

import javax.annotation.Resource;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class ValidEmailValidator implements ConstraintValidator<ValidEmail, String> {
    @Resource
    private CacheGateway cacheGateway;

    @Override
    public void initialize(ValidEmail constraintAnnotation) {
        // 初始化方法，通常不需要做任何事情
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // 实现校验逻辑
        if (value == null) {
            return false;
        }
        // 假设这里使用正则表达式来校验email的有效性
        String emailRegex = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$";
        boolean is = value.matches(emailRegex);
        if (!is) {
            return false;
        }

        return !cacheGateway.isBlack(value) ;
    }
}